#include "LZWDictionary.h"

LZWDictionary::LZWDictionary()
{
	this->_size = 0;
	for(unsigned int i = 0; i < 256; ++i)
		words[this->_size++] = vector<unsigned char>(1, i);
}

void LZWDictionary::addWord(vector<unsigned char> w)
{
	if(4096 <= size())
		return;

#ifdef __DEBUG__
	cout << "Adicionando ";
	for(unsigned int i = 0; i < w.size(); ++i)
		cout << (char)w[i];

	cout << " ao dic." << endl;
#endif
	words[this->_size++] = w;
}

unsigned short LZWDictionary::getBitCode(vector<unsigned char> word)
{
	unsigned short j;
	for(unsigned short i = 0; i < this->_size; ++i)
	{
		if(word.size() != words[i].size())
			continue;

		for(j = 0; j < word.size(); ++j)
			if(word[j] != words[i][j])
				break;

		if(j == word.size())
			return i;
	}

	return 65535;
}

unsigned int LZWDictionary::size()
{
	return this->_size;
}

vector<unsigned char> LZWDictionary::getWord(unsigned short i)
{
	return words[i];
}
